/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package clientprogram;

import java.awt.AWTException;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Console;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

/**
 *
 * @author ASUS
 */
public class ClientProgram {

    /**
     * @param args the command line arguments
     */
    User user;
    LoginForm loginForm;
    LockScreen lookScreen;
    ClientMain clientMain;
    Socket clientSocket;
    DataOutputStream outputStream;
    DataInputStream inputStream;
    
    public ClientProgram() throws IOException
    {
        InitConnection();
        LockScreen.Instance().Open();
    }
    
    public void InitConnection()
    {
        System.out.println("Init COnnection");
        try {
            System.out.println("Init socket!");
            //Readfile Config
            String ipString = "";
            File f = new File("ipconfig.txt");
            if (f.exists()){
                FileReader fr = new FileReader(f);
                BufferedReader br  = new BufferedReader(fr);
                ipString = br.readLine();
                while(ipString == null || ipString.isEmpty()){
                     ipString = JOptionPane.showInputDialog(null, "Nhập IP máy chủ", "Thông báo", JOptionPane.OK_OPTION);                     
                }                
                br.close();
            }
            else{
               if (f.createNewFile()){
                   while(ipString == null || ipString.isEmpty()){
                        ipString = JOptionPane.showInputDialog(null, "Nhập IP máy chủ", "Thông báo", JOptionPane.OK_OPTION);                     
                    }
               }
            }
            clientSocket = new Socket(ipString, 2014);
            System.out.println("Init Socket Comunication");  
            SocketCommunication.Instance().Init(clientSocket);
            System.out.println("Client khởi động thành công!");
            f = new File("ipconfig.txt");
            FileWriter fr = new FileWriter(f);
            BufferedWriter br  = new BufferedWriter(fr);
            br.write(ipString);
            br.close();
            
        } catch (Exception ex) {
            Logger.getLogger(ClientProgram.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Không thể kết nối đến Server!");
            File f = new File("ipconfig.txt");
            f.delete();
            InitConnection();
        }
    }
    
    public void ScreenCapture()
    {
        Robot robot;
        try {
            robot = new Robot();
            BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            try {
                ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
            } catch (IOException ex) {
                Logger.getLogger(ClientProgram.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (AWTException ex) {
            Logger.getLogger(ClientProgram.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void main(String[] args) throws Exception {
        ClientProgram client = new ClientProgram();  
    }
    
}
